{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7e14cf40-3539-46b7-964a-4fb61c177dd1",
   "metadata": {},
   "source": [
    "# <center> LangGraph快速入门与Agent开发实战\n",
    "# <center> Part 2.LangSmith工业级开发工具介绍"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c41b0b20-bdbb-4362-a1f0-60dbe1c95247",
   "metadata": {},
   "source": [
    "## 一、LangSmith 应用入门"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3fdd88f-f731-4372-b4e3-459175ce6496",
   "metadata": {},
   "source": [
    "&emsp;&emsp;大模型具有不确定性，尤其是构建复杂`AI Agent`应用程序中，中间会涉及非常多的子步骤，如果想要了解每一步的运行状态和结果，一方面可以通过`Debug`来进行实时控制，而另一方面可以借助一些工具来观察和调试中间的交互流程。`Langsmith`就是这样一个工具平台， 由​​ `LangChain` 和 `LangGraph` 背后的团队创建，**主要作用是：为基于大语言模型构建的应用程序提供全面的监控、调试和可观察性。提供强大的跟踪、日志记录和实时分析功能。**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00773406-c462-4c52-b544-9348b5514e19",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624185740544.png\" alt=\"image-20250624185740544\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80b2c89a-9904-4e1c-84f9-0eb221f4acf7",
   "metadata": {},
   "source": [
    "> LangSmith官网：https://smith.langchain.com/"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bc22ac7-fdbf-44ea-87d2-fc066d9c7c24",
   "metadata": {},
   "source": [
    "&emsp;&emsp;通常，对于一个项目而言，可以是单个应用程序或服务。该项目将包含多个跟踪，每个跟踪都是运行的集合 - 一个运行代表应用程序中的一个基本操作，例如对`DeepSeek`的`API`调用，或检索操作。如下图所示："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd0dfa02-4eb5-43aa-a870-016233df8599",
   "metadata": {},
   "source": [
    "<div align=center><img src=\"https://muyu001.oss-cn-beijing.aliyuncs.com/img/image-20241022164041124.png\" width=80%></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a082799-9fa2-4948-9f9a-a822103eeb63",
   "metadata": {},
   "source": [
    "- **Project (项目)：** 蓝色方块代表整个项目，可能是一个单独的应用程序或服务。\n",
    "- **Traces (轨迹)：** 绿色方块代表项目在不同条件或配置下的执行路径。每个轨迹可以是一个特定的用户会话、一个功能的执行，或者应用在特定输入下的行为。\n",
    "- **Runs (运行)：** 每个轨迹下的黄色方块表示特定轨迹的单次执行。这些是执行的实例，每个实例都是轨迹在特定条件下的实际运行。\n",
    "- **Feedback, Tags, Metadata (反馈、标签、元数据)：** 这部分显示了系统如何利用用户或自动化工具生成的反馈、标签和元数据来增强轨迹的管理和过滤。反馈可以用于改进未来的运行，标签和元数据可用于分类和筛选特定的轨迹或运行，以便在LangSmith的用户界面中更容易地管理和审查"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb8fd44d-d295-422c-854f-b23f010fdc2d",
   "metadata": {},
   "source": [
    "&emsp;&emsp;我们使用当前构建的 `LangGraph`图为例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9cfee744-ec70-4b0a-ad22-c101005b6e0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv \n",
    "load_dotenv(override=True)\n",
    "\n",
    "DeepSeek_API_KEY = os.getenv(\"DEEPSEEK_API_KEY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5e332e37-6efd-42bd-941a-ab5bc3090c65",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import init_chat_model\n",
    "\n",
    "model = init_chat_model(model=\"deepseek-chat\", model_provider=\"deepseek\")  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f0b10a05-c8c6-42dd-b27c-067cb921abfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Annotated\n",
    "from typing_extensions import TypedDict\n",
    "from langgraph.graph import StateGraph, START, END\n",
    "from langgraph.graph.message import add_messages\n",
    "from langchain.chat_models import init_chat_model\n",
    "from langgraph.checkpoint.memory import MemorySaver\n",
    "\n",
    "# 1. 定义状态类（会自动合并 messages）\n",
    "class State(TypedDict):\n",
    "    messages: Annotated[list, add_messages]\n",
    "\n",
    "# 2. 初始化模型\n",
    "model = init_chat_model(model=\"deepseek-chat\", model_provider=\"deepseek\")\n",
    "\n",
    "# 3. 定义聊天节点\n",
    "def chatbot(state: State) -> State:\n",
    "    reply = model.invoke(state[\"messages\"])\n",
    "    return {\"messages\": [reply]}\n",
    "\n",
    "# 4. 构建带 MemorySaver 的图\n",
    "builder = StateGraph(State)\n",
    "builder.add_node(\"chatbot\", chatbot)\n",
    "builder.add_edge(START, \"chatbot\")\n",
    "memory = MemorySaver()\n",
    "graph = builder.compile(checkpointer=memory)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c3a39d01-6360-42a9-8748-c2c0603a4b30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGoAAADqCAIAAADF80cYAAAAAXNSR0IArs4c6QAAFo5JREFUeJztnXl8E2XewJ/JJGnOJm2a0jP0skBLwZIeHFY5yuECIsdyo+y+vCyg+KKrLOiKCop8VhDUVY5FXF63iCvLWZCir7CUu0BbhNKW3vRu0ua+Zibz/hG3djHJpH2SNu0+37+aeWYmv3z7zMwzzzPz/DCapgGip7D6OoD+DdIHBdIHBdIHBdIHBdIHBRty++Yai1FHWYyUxURRRP9oA+EcjCfAeUJcJMEHDebB7ArrWbuv+q6x6q6x8o5BLGUHBnN4QpwnZHG4/aMuEza7xWg3GymdmjBqyfiRorjhwphkYQ921W19rQ+tF75pJaz2IWmBCY+LpHJOD77Vf9C0EQ8K9WU39QF81vhfh8qjArq1eTf0UQR98Whbbakpc1rwsMzAHkXrv9y7qrtxVh2XInp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uc4Y6Lobe5sthw9aw2akSYt2PrB9QXN4+dIY1za5ChSRyVKNC2mm0mX11A/BabmdS1maMTGW5YGQ7eAD5riDKwuaojarjzWzeKIt/aNtVpEUna2DjXaassMjxx9W93u//qbvHme9m0i7QidjvFYjk5/Suiklc+/7GrHbZWtA9JD+RwGc6qzENFZgN1cEtNTFoEz0VPfXtHo9PlFovB0eL9JTjOkQR681baVQwAABth5XKcDP2w2dxAsfMLvUVvq73dtPytmAA+w9Hp0Uhb4YWO2+d1sekRLNx/nyDwFnbSXl3QmD5ZMiKLuZPYIx2PPymVR3Dq77b54ZO83oWm6Yd3WkIiOCnjPBqc8EgfxsJ+9dtwDk41lw3wpCdNpe1cLj39v8IxlkdtSU8PRjYHm70mApDWuqIWu2eDeP0LO0nXFbVgdtvsNZFsj58Y6t5DGhRJf/vX5pY6myI1jMPrpaQnvQBhIWtvN0fEBUxdNghnd+MepidPWN0813Hzh44QhSRYIWHhvZTKxUdQFN1eq1HX6dImB6VlB3mwxb/RwwfUOlqIwn9qqu8aBVIBXxogkvHZXF/1DPoC0kIZOswmrdXcYYpLEaaOl0rlPekYhnq6lCTomnum8iLjw/sGGmA8EYcr4LAD/PSgpmlA2UibibAYbRgNFEmix1KFCSOgxhG99laRQUNq2gitivBkcL5vwIAwkC0J4UjlHJHUO/9jf3wpqx8x8O8ifArSBwXSBwXSBwXSBwXSB8X/A86fhONOxhYmAAAAAElFTkSuQmCC",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import Image, display\n",
    "\n",
    "display(Image(graph.get_graph().draw_mermaid_png()))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52fbcbbc-2c7b-4dbb-9c4b-2d582ae8d545",
   "metadata": {},
   "source": [
    "&emsp;&emsp;跟踪是请求通过图逻辑的步骤的详细记录。将其视为显示从开始到结束的确切顺序的路线图，在当前的设置中，`LangGraph` 很简单：\n",
    "- 它以`__start__`节点开始\n",
    "- 经过大模型生成回复的`chatbot`节点\n",
    "- 结束于`__end__`节点。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce32e860-df6d-406d-bf5e-2a46678e8403",
   "metadata": {},
   "source": [
    "&emsp;&emsp;每次用户与程序进行交互时，该图都会展开，并且此过程中的每个步骤都会记录为跟踪的一部分。借助 `Langsmith`，我们可以实时可视化和分析这些痕迹。设置 `Langsmith`并不复杂，我们需要依次执行如下操作步骤。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "704a6209-fdb8-41f5-b439-bf921e4418a1",
   "metadata": {},
   "source": [
    "- **Step 1. 创建一个 LangSmith 帐户**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afe2ecb3-0a45-4e85-9763-f10561dd7c17",
   "metadata": {},
   "source": [
    "&emsp;&emsp;要开始使用 `LangSmith`，我们需要创建一个帐户。可以在这里注册一个免费帐户进入`LangSmith`登录页面： https://smith.langchain.com/ ， 支持使用 Google、GitHub、Discord 和电子邮件登录。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0280455a-8f54-47ca-880e-f1443dd953e7",
   "metadata": {},
   "source": [
    "<center><div align=center><img src=\"https://muyu20241105.oss-cn-beijing.aliyuncs.com/images/202506151835039.png\" width=33%></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25dd6866-00b3-42a9-9260-5efafe39bb74",
   "metadata": {},
   "source": [
    "&emsp;&emsp;注册并等登录后，可以直接查看到仪表板："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63a4cf67-b518-48fe-a843-95ba78500179",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624190009348.png\" alt=\"image-20250624190009348\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0369e0c-b666-4614-97c1-65626ebcbdef",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624190049558.png\" alt=\"image-20250624190049558\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22fa0ab6-0b65-46c8-ae04-c7108553f554",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624190123279.png\" alt=\"image-20250624190123279\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80ad226e-c8b5-4d86-820f-ccb42229e8d9",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624190228537.png\" alt=\"image-20250624190228537\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cee666d9-06f3-4404-bd09-448f41754048",
   "metadata": {},
   "source": [
    "&emsp;&emsp;在构建程序跟踪前，首先需要创建一个 `API` 密钥，该密钥将允许我们的项目开始向 `Langsmith` 发送跟踪数据。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e3bb4fd-8b93-4918-8d2b-8d958344fac4",
   "metadata": {},
   "source": [
    "- **Step 2. 已创建账号的API密钥创建流程**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6a919aa-e1a5-4051-b6be-6b1591ca88d6",
   "metadata": {},
   "source": [
    "&emsp;&emsp;单击 `Langsmith` 仪表板左侧菜单中的“设置”图标。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0c5668f-f423-49d3-8bba-712474d6d41c",
   "metadata": {},
   "source": [
    "<div align=center><img src=\"https://muyu20241105.oss-cn-beijing.aliyuncs.com/images/202506151835041.png\" width=80%></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad909646-18f5-4cb0-b678-9563c5db937a",
   "metadata": {},
   "source": [
    "&emsp;&emsp;导航至“API 密钥”部分，然后单击`Create API Key` 。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d1806d8-e4f6-4314-8c5f-fe1dfb83753b",
   "metadata": {},
   "source": [
    "<div align=center><img src=\"https://muyu20241105.oss-cn-beijing.aliyuncs.com/images/202506151835042.png\" width=80%></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "924f999b-76a2-41ad-81a0-f3531fb4b624",
   "metadata": {},
   "source": [
    "&emsp;&emsp;`LangSmith` 支持两种类型的 API 密钥：服务密钥和个人访问令牌。两种类型的令牌都可用于验证对 `LangSmith API` 的请求，但它们有不同的用例。这里选择“密钥类型令牌的个人访问” ，因为我们将使用此密钥作为用户进行个人访问。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d32e703a-270c-4dc8-8f76-2eb12b18ee37",
   "metadata": {},
   "source": [
    "<div align=center><img src=\"https://muyu20241105.oss-cn-beijing.aliyuncs.com/images/202506151835043.png\" width=80%></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcb8e3f7-47a4-4f55-a537-441d7f05e047",
   "metadata": {},
   "source": [
    "&emsp;&emsp;单击“创建 API 密钥” ，复制并确认已保存。这和`DeepSeek`的`API`密钥一样，一旦创建完成，则不允许再次复制。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76b2e1ac-08ed-4475-8bc9-fdc24e07b356",
   "metadata": {},
   "source": [
    "<div align=center><img src=\"https://muyu20241105.oss-cn-beijing.aliyuncs.com/images/202506151835044.png\" width=80%></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8cc941f-0cbd-410d-b449-e86afa8e4439",
   "metadata": {},
   "source": [
    "&emsp;&emsp;注意：退出后无法再复制，只能删除重建。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d1998e3-32b5-428d-bd14-0c81c391236b",
   "metadata": {},
   "source": [
    "&emsp;&emsp;现在，就可以将其集成到我们的项目中了。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "560cd1a1-f39e-4f8c-8cd5-e68bc92b57d3",
   "metadata": {},
   "source": [
    "- **Step 3. 创建环境变量**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c60ba20c-3785-4482-b95d-a19999de4313",
   "metadata": {},
   "source": [
    "&emsp;&emsp;将`LANGCHAIN_API_KEY`替换为我们刚刚创建的 `API` 密钥。定义的方法如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0255785f-0543-4084-a32c-2dc815754c57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true\n",
      "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\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "# 设置环境变量\n",
    "os.environ[\"LANGSMITH_TRACING\"]  = \"true\"                     # 开启追踪\n",
    "os.environ[\"LANGSMITH_API_KEY\"]  = \"lsv2_pt_***\" # 注意：这里替换为自己的LangSmith API Key\n",
    "os.environ[\"LANGSMITH_PROJECT\"]  = \"langraph_chatbot\"         # 任意项目名\n",
    "\n",
    "# 验证环境变量是否设置成功\n",
    "print(os.getenv(\"LANGSMITH_TRACING\"))\n",
    "print(os.getenv(\"LANGSMITH_API_KEY\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56f4c27f-817e-40ea-8f6e-75281cda8c41",
   "metadata": {},
   "source": [
    "&emsp;&emsp;在环境变量中设置以后，我们的代码不需要做任何的改变，即可构建`LangSmith`的消息跟踪。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1ca497e5-942a-4bf3-8b08-f5e32c29dcc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv \n",
    "load_dotenv(override=True)\n",
    "\n",
    "DeepSeek_API_KEY = os.getenv(\"DEEPSEEK_API_KEY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5e4a4307-d81b-444e-a18b-69c596a86f96",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import init_chat_model\n",
    "\n",
    "model = init_chat_model(model=\"deepseek-chat\", model_provider=\"deepseek\")  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "55297e93-012a-4b66-a73a-51776c7aa145",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Annotated\n",
    "from typing_extensions import TypedDict\n",
    "from langgraph.graph import StateGraph, START, END\n",
    "from langgraph.graph.message import add_messages\n",
    "from langchain.chat_models import init_chat_model\n",
    "from langgraph.checkpoint.memory import MemorySaver\n",
    "\n",
    "# 1. 定义状态类（会自动合并 messages）\n",
    "class State(TypedDict):\n",
    "    messages: Annotated[list, add_messages]\n",
    "\n",
    "# 2. 初始化模型\n",
    "model = init_chat_model(model=\"deepseek-chat\", model_provider=\"deepseek\")\n",
    "\n",
    "# 3. 定义聊天节点\n",
    "def chatbot(state: State) -> State:\n",
    "    reply = model.invoke(state[\"messages\"])\n",
    "    return {\"messages\": [reply]}\n",
    "\n",
    "# 4. 构建带 MemorySaver 的图\n",
    "builder = StateGraph(State)\n",
    "builder.add_node(\"chatbot\", chatbot)\n",
    "builder.add_edge(START, \"chatbot\")\n",
    "memory = MemorySaver()\n",
    "graph = builder.compile(checkpointer=memory)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "301e2344-73fa-49d2-b7a1-914712c74c7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGoAAADqCAIAAADF80cYAAAAAXNSR0IArs4c6QAAFo5JREFUeJztnXl8E2XewJ/JJGnOJm2a0jP0skBLwZIeHFY5yuECIsdyo+y+vCyg+KKrLOiKCop8VhDUVY5FXF63iCvLWZCir7CUu0BbhNKW3vRu0ua+Zibz/hG3djHJpH2SNu0+37+aeWYmv3z7zMwzzzPz/DCapgGip7D6OoD+DdIHBdIHBdIHBdIHBdIHBRty++Yai1FHWYyUxURRRP9oA+EcjCfAeUJcJMEHDebB7ArrWbuv+q6x6q6x8o5BLGUHBnN4QpwnZHG4/aMuEza7xWg3GymdmjBqyfiRorjhwphkYQ921W19rQ+tF75pJaz2IWmBCY+LpHJOD77Vf9C0EQ8K9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MfQ+UxmcpZY9Jnu98kaci4X0154VrBsX9ezgmShi+cs2n3gdV2u08OEb3KFDuc4Y6Lobe5sthw9aw2akSYt2PrB9QXN4+dIY1za5ChSRyVKNC2mm0mX11A/BabmdS1maMTGW5YGQ7eAD5riDKwuaojarjzWzeKIt/aNtVpEUna2DjXaassMjxx9W93u//qbvHme9m0i7QidjvFYjk5/Suiklc+/7GrHbZWtA9JD+RwGc6qzENFZgN1cEtNTFoEz0VPfXtHo9PlFovB0eL9JTjOkQR681baVQwAABth5XKcDP2w2dxAsfMLvUVvq73dtPytmAA+w9Hp0Uhb4YWO2+d1sekRLNx/nyDwFnbSXl3QmD5ZMiKLuZPYIx2PPymVR3Dq77b54ZO83oWm6Yd3WkIiOCnjPBqc8EgfxsJ+9dtwDk41lw3wpCdNpe1cLj39v8IxlkdtSU8PRjYHm70mApDWuqIWu2eDeP0LO0nXFbVgdtvsNZFsj58Y6t5DGhRJf/vX5pY6myI1jMPrpaQnvQBhIWtvN0fEBUxdNghnd+MepidPWN0813Hzh44QhSRYIWHhvZTKxUdQFN1eq1HX6dImB6VlB3mwxb/RwwfUOlqIwn9qqu8aBVIBXxogkvHZXF/1DPoC0kIZOswmrdXcYYpLEaaOl0rlPekYhnq6lCTomnum8iLjw/sGGmA8EYcr4LAD/PSgpmlA2UibibAYbRgNFEmix1KFCSOgxhG99laRQUNq2gitivBkcL5vwIAwkC0J4UjlHJHUO/9jf3wpqx8x8O8ifArSBwXSBwXSBwXSBwXSB8X/A86fhONOxhYmAAAAAElFTkSuQmCC",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import Image, display\n",
    "\n",
    "display(Image(graph.get_graph().draw_mermaid_png()))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c70f481a-e289-4bd3-b2c5-c508401dafbf",
   "metadata": {},
   "source": [
    "&emsp;&emsp;接下来正常进行多轮对话的问答，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bce0f222-5d50-4fa4-8fc6-4e2675abbefb",
   "metadata": {},
   "outputs": [],
   "source": [
    "thread_config = {\"configurable\": {\"thread_id\": \"session_1\"}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6ef37afb-fd3c-4e81-96de-3f37fe79691f",
   "metadata": {},
   "outputs": [],
   "source": [
    "state1 = graph.invoke({\"messages\": [{\"role\":\"user\",\"content\":\"你好，好久不见，我叫陈明。\"}]}, config=thread_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "886a476c-9a45-42ee-a7ba-e67b75d7738c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'messages': [HumanMessage(content='你好，好久不见，我叫陈明。', additional_kwargs={}, response_metadata={}, id='7df2fb6d-2bdb-4f12-8567-a0d328a0b1c3'),\n",
       "  AIMessage(content='你好，陈明！确实好久不见啦～很高兴再次见到你！最近过得怎么样？有什么新鲜事想分享吗？或者有什么我可以帮你的呢？😊', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 35, 'prompt_tokens': 12, 'total_tokens': 47, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 12}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_8802369eaa_prod0623_fp8_kvcache', 'id': '44f5c397-572e-47fa-a1dd-43cccc37d72e', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--08d7a354-06a8-4f2e-95a7-3c3c28713617-0', usage_metadata={'input_tokens': 12, 'output_tokens': 35, 'total_tokens': 47, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}})]}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c82299f6-afdd-4ded-99ef-dd856972b47a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='你好，好久不见，我叫陈明。', additional_kwargs={}, response_metadata={}, id='7df2fb6d-2bdb-4f12-8567-a0d328a0b1c3'),\n",
       " AIMessage(content='你好，陈明！确实好久不见啦～很高兴再次见到你！最近过得怎么样？有什么新鲜事想分享吗？或者有什么我可以帮你的呢？😊', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 35, 'prompt_tokens': 12, 'total_tokens': 47, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 12}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_8802369eaa_prod0623_fp8_kvcache', 'id': '44f5c397-572e-47fa-a1dd-43cccc37d72e', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--08d7a354-06a8-4f2e-95a7-3c3c28713617-0', usage_metadata={'input_tokens': 12, 'output_tokens': 35, 'total_tokens': 47, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}})]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state1['messages']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3482b9c8-bce0-4c4a-8d5a-2f5331f358da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好，陈明！确实好久不见啦～很高兴再次见到你！最近过得怎么样？有什么新鲜事想分享吗？或者有什么我可以帮你的呢？😊'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state1['messages'][-1].content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3bb2eec-5a29-49fb-aaa0-4322dea51af0",
   "metadata": {},
   "source": [
    "&emsp;&emsp;当触发`Graph`执行时，可以在`LangSmith`控制面板上实时查看到该应用程序执行过程中各个状态的输入和输出情况。如下图所示："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27b49780-1c40-4bcc-a360-c4b7b7a94f8a",
   "metadata": {},
   "source": [
    "<center>\n",
    "\n",
    "<img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624190558402.png\" alt=\"image-20250624190558402\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bd2c87f-6cc3-4153-a912-5373a9b09528",
   "metadata": {},
   "source": [
    "&emsp;&emsp;点击进去可以查看到更加具体的追踪信息："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70227897-67a9-4517-ada5-2d797e1e62f0",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624190640491.png\" alt=\"image-20250624190640491\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf858a9b-d09c-4291-97c6-dff057b36654",
   "metadata": {},
   "source": [
    "&emsp;&emsp;至此，大家已经可以运行起`LangSmith`应用工具。在初次构建AI应用程序时，建议从最简单的工作流实现开始，结合 `LangSmith` 这样的评估工具是能够极大程度上帮助大家理解其中间过程。而随着构建应用程序的越来越复杂，其中间状态也会变得越来越多。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5dd1a035-b8a3-44a0-a770-99ca0899d413",
   "metadata": {},
   "source": [
    "## 二、LangGraph Studio 可视化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a42cf70-4139-4bc2-83d3-0a52700182a0",
   "metadata": {},
   "source": [
    "&emsp;&emsp;`LangSmith` 与 `LangGraph Studio` 都是 `LangChain AI` 生态中非常核心的工具，前者是用于跟踪和分析大模型的使用情况，而`langGraph Studio`则是对于`LangGraph`来说，则是比`LangSmith`更加方便和高效的可视化调试工具平台。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebe6a9f3-ca66-425e-b156-3ac454ea9b67",
   "metadata": {},
   "source": [
    "&emsp;&emsp;大家可以思考一个问题：基于`LangGraph`框架可以开发出各种复杂的应用、`Agent`、`Workflow`等，那么这些应用、`Agent`、`Workflow`等在生产中如何部署和运行呢？ 解决方案就是把它们部署成一个`Server`。 而对如何方便高效的把`LangGraph`的`Graph`部署成一个`Server`，`LangGraph`官方提供了`LangGraph Platform`，其完整架构如下所示："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48f8a2e9-2b2f-41cf-aeb0-ec3a44e81ee7",
   "metadata": {},
   "source": [
    "<div align=center><img src=\"https://muyu20241105.oss-cn-beijing.aliyuncs.com/images/202506161127412.png\" width=80%></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8c85b4a-a383-4534-92a5-9cc201899796",
   "metadata": {},
   "source": [
    "- **LangGraph Studio**：桌面版应用（`目前仅支持Mac`）和本地运行（适用于所有操作系统）；\n",
    "- **LangServer**：最终构建出来的服务，提供`Assistant API`接口；\n",
    "- **Python/JS SDK**：通过接口可以直接和 `LangServer` 提供的各个`API`接口连接；\n",
    "- **Remote Graph**：类似于之前`LangServe`的用法，可以直接用 `Graph` 的接口去调用，这样拿到的`Graph`就是一个 `Runable`对象，就可以去调用它的`invoke`，`batch` 等。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad7038cc-c29e-4fe8-8022-bc8335f3cc34",
   "metadata": {},
   "source": [
    "&emsp;&emsp;`LangGraph Studio` 是专为 `LangGraph` 图式代理打造的本地/云端 `IDE`，具备可视化节点和状态及实时调试功能。`LangGraph Studio` 在本地可视化运行时会自动把调用过程上传到 `LangSmith`；而在 `LangSmith` 网页端查看任何 `Trace` 时，又能一键`Run in Studio`回放整条执行链，所以它是通过统一 `Trace SDK` 与 `LangSmith` 紧密集成。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7356b25-b4a9-481d-adcc-a188cb7a8b03",
   "metadata": {},
   "source": [
    "&emsp;&emsp;接下来我们就来详细的讲解下如何使用`LangGraph Studio`。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5752eb27-2068-46e0-975e-0532293638db",
   "metadata": {},
   "source": [
    "&emsp;&emsp;首先需要说明的是，如果想要把一个定义的`Graph`添加到`LangGraph Studio`中，需要严格按照官方要求的项目结构进行构建，需要执行的步骤依次是："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14ae6d1c-af70-45f1-b1db-444e33ea4068",
   "metadata": {},
   "source": [
    "- **Step 1. 创建一个`LangGraph`项目文件夹**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "002db010-6a3a-4b9a-981c-0d6fee8be0d6",
   "metadata": {},
   "source": [
    "&emsp;&emsp;我们这里创建一个`langraph_chatbot`文件夹，如下图所示："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a04b072-268d-4423-a918-335e91fb892d",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624190858497.png\" alt=\"image-20250624190858497\" style=\"zoom:50%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77e1625c-1c25-48ae-83fa-cb3b89e7c831",
   "metadata": {},
   "source": [
    "- **Step 2. 创建`requirements.txt`文件**\n",
    "\n",
    "&emsp;&emsp;在`langgraph_chatbot`文件夹中，新建一个`requirements.txt`文件，里面需要填写在运行该项目时需要安装的依赖项，如下所示："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb2ee9e9-b33e-4507-9e42-e664eaa2c0fc",
   "metadata": {},
   "source": [
    "```bash\n",
    "langgraph\n",
    "langchain-core\n",
    "langchain-deepseek\n",
    "python-dotenv\n",
    "langsmith\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8aec0328-284c-4d6e-b25e-fbe531576a7e",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624191027491.png\" alt=\"image-20250624191027491\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b66745d5-518e-465a-abbc-6b4b9d08d539",
   "metadata": {},
   "source": [
    "- **Step 3. 创建`.env`文件**\n",
    "\n",
    "&emsp;&emsp;在`langgraph_chatbot`文件夹中，新建一个`.env`文件，将敏感信息（如`API`密钥）放在环境变量中而不是硬编码。如下所示："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea59b103-b0a8-4b33-b696-af490e4b4f84",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624191259742.png\" alt=\"image-20250624191259742\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ef6cd79-b7d7-450b-9484-1135ede16ba1",
   "metadata": {},
   "source": [
    "- **Step 4. 创建`graph.py`文件**\n",
    "\n",
    "&emsp;&emsp;在`langgraph_chatbot`文件夹中，新建一个`graph.py`文件，在该文件中编写构建图的具体运行逻辑，如状态、节点、变、图的编译等。注意：这里不要使用`init_chat_model`方法，而是使用`ChatDeepSeek`方法，因为会存在异步阻塞问题。代码如下所示："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de216620-3ab4-42a6-ab28-3ce1413c05da",
   "metadata": {},
   "source": [
    "```python\n",
    "from langgraph.graph import StateGraph, START, END\n",
    "from langgraph.graph.message import add_messages\n",
    "from langchain_deepseek import ChatDeepSeek\n",
    "from typing import Annotated\n",
    "from typing_extensions import TypedDict\n",
    "import os\n",
    "from dotenv import load_dotenv \n",
    "\n",
    "# 加载环境变量\n",
    "load_dotenv(override=True)\n",
    "\n",
    "class State(TypedDict):\n",
    "    \"\"\"聊天机器人的状态定义\"\"\"\n",
    "    messages: Annotated[list, add_messages]\n",
    "\n",
    "def chatbot(state: State):\n",
    "    \"\"\"聊天机器人节点函数\"\"\"\n",
    "    model = ChatDeepSeek(model=\"deepseek-chat\")\n",
    "    return {\"messages\": [model.invoke(state[\"messages\"])]}\n",
    "\n",
    "# 构建图\n",
    "graph_builder = StateGraph(State)\n",
    "graph_builder.add_node(\"chatbot\", chatbot)\n",
    "graph_builder.add_edge(START, \"chatbot\")\n",
    "graph_builder.add_edge(\"chatbot\", END)\n",
    "\n",
    "# 编译图\n",
    "graph = graph_builder.compile()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ed5a1a0-6821-4038-906e-8397973ed952",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624191412113.png\" alt=\"image-20250624191412113\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15b179c3-3ecf-4b7a-9e64-b64b408ffd3b",
   "metadata": {},
   "source": [
    "- **Step 5. 创建`langgraph.json`文件**\n",
    "\n",
    "&emsp;&emsp;在`langgraph_chatbot`文件夹中，新建一个`langgraph.json`文件，在该`json`文件中配置项目信息，遵循规范如下所示：\n",
    "\n",
    "- 必须包含 `dependencies` 和 `graphs` 字段\n",
    "- `graphs` 字段格式：\"图名\": \"文件路径:变量名\"\n",
    "- 配置文件必须放在与Python文件同级或更高级的目录\n",
    "\n",
    "&emsp;&emsp;注意: 项目文件的名称必须为`langgraph.json`。如下所示："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "caf57fe9-8cf6-4b06-a3da-cd92d2026b7f",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624191532218.png\" alt=\"image-20250624191532218\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db3ddc29-0f19-497a-a8d4-61b98d4bb808",
   "metadata": {},
   "source": [
    "&emsp;&emsp;其中：\n",
    "- `dependencies`: [\"./\"] - 告诉`LangGraph`在当前目录查找依赖项（会自动读取`requirements.txt`）\n",
    "- `chatbot`: \"./graph.py:graph\" - 定义图名为`chatbot`，来自`graph.py`文件中的`graph`变量\n",
    "- `env`: \".env\" - 指定环境变量文件位置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d104e37-4934-48f0-b4b4-e1da3522161d",
   "metadata": {},
   "source": [
    "&emsp;&emsp;最终完整项目结构如下所示：\n",
    "\n",
    "```json\n",
    "    ./langraph_chatbot/\n",
    "    ├── graph.py              # 对应官方的 agent.py\n",
    "    ├── requirements.txt      # ✅ 依赖管理\n",
    "    ├── langgraph.json       # ✅ 配置文件\n",
    "    └── .env                 # ✅ 环境变量\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63b7c63e-7e44-4023-91c8-2c715965c7b8",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624191613003.png\" alt=\"image-20250624191613003\" style=\"zoom:50%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d169053-e84d-4951-a259-7156c6842153",
   "metadata": {},
   "source": [
    "&emsp;&emsp;完整的代码已经上传至百度网盘中的`langgraph_chatbot`文件夹中，大家可以扫描下方二维码免费领取"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8e135c9-81c8-4c06-9b2e-e914a2631445",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624191855635.png\" alt=\"image-20250624191855635\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89118e04-fcf6-43d3-81d4-022a8fa94dd3",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/e873ea36c8a2263b2a88a0f86f9481b.png\" alt=\"e873ea36c8a2263b2a88a0f86f9481b\" style=\"zoom:50%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e999742-35a6-4d6f-a1ec-d8bb567af8cb",
   "metadata": {},
   "source": [
    "&emsp;&emsp;然后，安装`langgraph-cli`依赖，执行如下代码：\n",
    "\n",
    "```bash\n",
    "    pip install -U \"langgraph-cli[inmem]\" -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53164ecd-09f8-40c3-ac6b-ad5f5eaaed34",
   "metadata": {},
   "source": [
    "<center>\n",
    "\n",
    "<img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624192025273.png\" alt=\"image-20250624192025273\" style=\"zoom:50%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c55737d-9bdd-41aa-a603-e267778df1e8",
   "metadata": {},
   "source": [
    "&emsp;&emsp;最后，进入到`langgraph_chatbot`文件夹，执行`LangGraph dev`即可启动项目\n",
    "\n",
    "```bash\n",
    "   langgraph dev\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4f9d375-424f-44c9-aa3d-b30f39d86d4c",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624192113062.png\" alt=\"image-20250624192113062\" style=\"zoom:50%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89ca2344-8cdb-4915-98f3-929afda6fa20",
   "metadata": {},
   "source": [
    "&emsp;&emsp;启动成功后，即可点击`Studio UI`中显示的链接，在浏览器中打开并访问`Studio`，如下所示："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "657f2ed4-8e85-4fa9-b6d1-d4efd085ce4c",
   "metadata": {},
   "source": [
    "<center><img src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/img/image-20250624195356912.png\" alt=\"image-20250624195356912\" style=\"zoom:33%;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93b2cdeb-1391-4c2e-afa8-27258e1a993f",
   "metadata": {},
   "source": [
    "完整演示流程如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "bde0af5a-761a-4340-a796-cfa93d22174b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<video src=\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/LangSmith%26Studio%20%E9%97%AE%E7%AD%94%E6%9C%BA%E5%99%A8%E4%BA%BA%E5%8F%AF%E8%A7%86%E5%8C%96%E8%B0%83%E8%AF%95.mp4\" controls  width=\"800\"  height=\"400\">\n",
       "      Your browser does not support the <code>video</code> element.\n",
       "    </video>"
      ],
      "text/plain": [
       "<IPython.core.display.Video object>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Video(\"https://ml2022.oss-cn-hangzhou.aliyuncs.com/LangSmith%26Studio%20%E9%97%AE%E7%AD%94%E6%9C%BA%E5%99%A8%E4%BA%BA%E5%8F%AF%E8%A7%86%E5%8C%96%E8%B0%83%E8%AF%95.mp4\", width=800, height=400)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0rc2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
